|
1
|
|
|
/* globals Ziggurat */ |
|
2
|
|
|
'use strict'; |
|
3
|
|
|
|
|
4
|
|
|
angular |
|
5
|
|
|
.module('game') |
|
6
|
|
|
.service('util', ['numberFilter', |
|
7
|
|
|
'$sce', |
|
8
|
|
|
'$locale', |
|
9
|
|
|
'data', |
|
10
|
|
|
function(numberFilter, $sce, $locale, data) { |
|
11
|
|
|
let formats = $locale.NUMBER_FORMATS; |
|
12
|
|
|
this.gaussian = new Ziggurat(); |
|
13
|
|
|
this.poisson = new Poisson(); |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
this.getHTML = function(resource) { |
|
16
|
|
|
let html = data.html[resource]; |
|
17
|
|
|
if (typeof html === 'undefined') { |
|
18
|
|
|
html = data.resources[resource].html; |
|
19
|
|
|
} |
|
20
|
|
|
if (typeof html === 'undefined') { |
|
21
|
|
|
return resource; |
|
22
|
|
|
} |
|
23
|
|
|
return html; |
|
24
|
|
|
}; |
|
25
|
|
|
|
|
26
|
|
|
this.prettifyNumber = function(number) { |
|
27
|
|
|
if (typeof number === 'undefined' || number === null) { |
|
28
|
|
|
return null; |
|
29
|
|
|
} |
|
30
|
|
|
if (number === '') { |
|
31
|
|
|
return ''; |
|
32
|
|
|
} |
|
33
|
|
|
if (number === Infinity) { |
|
34
|
|
|
return '∞'; |
|
35
|
|
|
} |
|
36
|
|
|
if (number > 1e6) { |
|
37
|
|
|
// Very ugly way to extract the mantisa and exponent from an exponential string |
|
38
|
|
|
let exponential = number.toPrecision(6).split('e'); |
|
39
|
|
|
let exponent = parseFloat(exponential[1].split('+')[1]); |
|
40
|
|
|
// And it is displayed in with superscript |
|
41
|
|
|
return numberFilter(exponential[0], 4) + |
|
42
|
|
|
' × 10<sup>' + |
|
43
|
|
|
this.prettifyNumber(exponent) + |
|
44
|
|
|
'</sup>'; |
|
45
|
|
|
} |
|
46
|
|
|
return mangleCeroes(number, 4); |
|
47
|
|
|
}; |
|
48
|
|
|
|
|
49
|
|
|
// FIXME: it turns out we need this abomination since default numberFilter |
|
50
|
|
|
// uses 3 decimals and we need 4 (for the isotopes proportions precision) |
|
51
|
|
|
// and if you use decimals, it attaches 0's at the end |
|
52
|
|
|
function mangleCeroes(input, fractionSize) { |
|
53
|
|
|
//Get formatted value |
|
54
|
|
|
|
|
55
|
|
|
let formattedValue = numberFilter(input, fractionSize); |
|
56
|
|
|
//get the decimalSepPosition |
|
57
|
|
|
let decimalIdx = formattedValue.indexOf(formats.DECIMAL_SEP); |
|
58
|
|
|
//If no decimal just return |
|
59
|
|
|
if (decimalIdx === -1) { |
|
60
|
|
|
return formattedValue; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
let whole = formattedValue.substring(0, decimalIdx); |
|
64
|
|
|
let decimal = (Number(formattedValue.substring(decimalIdx)) || '').toString(); |
|
65
|
|
|
|
|
66
|
|
|
return whole + decimal.substring(1); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
this.randomDraw = function(number, p) { |
|
70
|
|
|
let production; |
|
71
|
|
|
let mean = number * p; |
|
72
|
|
|
if (p < 0.01) { |
|
73
|
|
|
// using Poisson distribution (would get slow for large numbers. there are fast formulas but I don't know |
|
74
|
|
|
// how good they are) |
|
75
|
|
|
production = this.poisson.getPoisson(mean); |
|
76
|
|
|
} else { |
|
77
|
|
|
// Gaussian distribution |
|
78
|
|
|
let q = 1 - p; |
|
79
|
|
|
let variance = number * p * q; |
|
80
|
|
|
let std = Math.sqrt(variance); |
|
81
|
|
|
production = Math.round(this.gaussian.nextGaussian() * std + mean); |
|
82
|
|
|
} |
|
83
|
|
|
if (production > number) { |
|
84
|
|
|
production = number; |
|
85
|
|
|
} |
|
86
|
|
|
if (production < 0) { |
|
87
|
|
|
production = 0; |
|
88
|
|
|
} |
|
89
|
|
|
return production; |
|
90
|
|
|
}; |
|
91
|
|
|
|
|
92
|
|
|
this.trustHTML = function(html) { |
|
93
|
|
|
return $sce.trustAsHtml(html); |
|
94
|
|
|
}; |
|
95
|
|
|
} |
|
96
|
|
|
]); |
|
97
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.